package com.haimi.shiguang.api;

import android.support.annotation.NonNull;
import android.util.Log;

import com.haimi.shiguang.BuildConfig;
import com.haimi.shiguang.base.biz.BaseResponse;
import com.haimi.shiguang.bean.request.LoginRequset;
import com.haimi.shiguang.bean.request.RegisterRequset;
import com.haimi.shiguang.bean.response.CenterResponse;
import com.haimi.shiguang.bean.response.LoginResponse;
import com.haimi.shiguang.framework.Constans;
import com.haimi.shiguang.framework.GlobalCache;
import com.haimi.shiguang.framework.HeaderCache;
import com.haimi.shiguang.net.AppendHeaderParamIntercepter;
import com.haimi.shiguang.net.RetrofitHelper;
import com.haimi.shiguang.net.UrlConstance;
import com.haimi.shiguang.utils.AesUtil;
import com.haimi.shiguang.utils.Md5Util;
import com.haimi.shiguang.utils.Store;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * @author changsunhaipeng
 * @date 2018/6/14
 */

public class ApiServeice {
    private static ApiServeice apiServeice = null;

    public static ApiServeice getInstance() {
        synchronized (ApiServeice.class) {
            if (apiServeice == null) {
                apiServeice = new ApiServeice();
            }
        }
        return apiServeice;
    }

    public Observable<BaseResponse> register(String mobile, String password, String code) {
        password = Md5Util.MD5EncodeUtf8(password, mobile, true);
        String requestId = HeaderCache.requestId;
        OkHttpClient okHttpClient = getOkHttpClient(requestId);
        String md5Encode = Md5Util.MD5EncodeUtf8(requestId, "", false);
        password = AesUtil.aesEncrypt(password, md5Encode);
        RegisterRequset registerRequset = new RegisterRequset();
        registerRequset.mobile = mobile;
        registerRequset.code = code;
        registerRequset.password = password;

        return RetrofitHelper.getInstance().getApiService(UrlConstance.BASE_URL, ApiInterface.class, okHttpClient)
                .register(registerRequset)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<BaseResponse<LoginResponse>> login(String mobile, String password) {
        password = Md5Util.MD5EncodeUtf8(password, mobile, true);
        String requestId = HeaderCache.requestId;
        OkHttpClient okHttpClient = getOkHttpClient(requestId);
        String md5Encode = Md5Util.MD5EncodeUtf8(requestId, "", false);
        password = AesUtil.aesEncrypt(password, md5Encode);
        LoginRequset loginRequset = new LoginRequset();
        loginRequset.mobile = mobile;
        loginRequset.password = password;

        return RetrofitHelper.getInstance().getApiService(UrlConstance.BASE_URL, ApiInterface.class, okHttpClient)
                .login(loginRequset)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @NonNull
    private OkHttpClient getOkHttpClient(String requestId) {
        //日志显示级别
        HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (BuildConfig.DEV) {
                    Log.d("retrofit", message);
                }
            }
        });
        loggingInterceptor.setLevel(level);
        AppendHeaderParamIntercepter appendHeaderParamIntercepter = new AppendHeaderParamIntercepter(requestId);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //OkHttp进行添加拦截器loggingInterceptor
        builder.addInterceptor(loggingInterceptor);
        builder.addInterceptor(appendHeaderParamIntercepter);
        builder.connectTimeout(60, TimeUnit.SECONDS);
        builder.readTimeout(30, TimeUnit.SECONDS);
        return builder.build();
    }

    public Observable<BaseResponse<CenterResponse>> center() {
        return RetrofitHelper.getInstance().getApiService(ApiInterface.class)
                .center(Store.getString(GlobalCache.getContext(), Constans.UID))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}
